/* 
 * File:   server.hpp
 * Author: bag
 *
 * Created on February 16, 2012, 1:18 PM
 */

#ifndef SERVER_HPP
#define	SERVER_HPP

#ifdef _DEBUG
#include <iostream>
#endif

#include "stdafx.hpp"

namespace bagprod { namespace finance { namespace SPS {
    
#define MAX_THREAD 2
#define MAX_CLIENT_PER_THREAD 5
    

// Function pointer types
typedef std::list<types::StockData*>* (*GetStockDataByTicker)(std::string);
typedef std::string (*GetCompanyName)(std::string);

class Server;

typedef struct {
	pthread_t tid;
	int client_count;
	int clients[MAX_CLIENT_PER_THREAD];
        int id;
        Server *server;
} Thread;

class Server
{
private:
    int socketFd;
    struct sockaddr_in server;
    Thread mThreads[MAX_THREAD];
    void CreateThreads();
    int ChooseThread();
public:
    Server();
    void Initialize(std::string host, int port);
    void Run();
    
    // Call backs
    GetStockDataByTicker f_ptrGetStockDataByTicker;
    GetCompanyName f_ptrGetCompanyName;
};

// Threading methods
void *server_thread_function(void *arg);
void HandleStockSnapshotRequest(Thread *thread, int fd, 
        const std::string &ticker);

}}} // namespace
#endif	/* SERVER_HPP */

